using System;
using System.Collections.Generic;
using System.Linq;
namespace TemplateWeb.Controllers
{
	#region using
	using System.Web.Mvc;
	using TemplateWeb.Models.ViewModels;
	using TemplateWeb.Models;
	using TemplateWeb.Models.Repositories;
	using TemplateWeb.Helpers;
	using System.Web.Security;
	#endregion
	public partial class AdminController: BaseController
	{
		public ActionResult SelectGocChiaSe()
		{
			var data = new DataTransferViewModel
			{
				EnumViewModelType = EnumViewModel.ADMIN_GOCCHIASE
			};
			return View(CreateViewModel(data));
		}



		[AcceptVerbs(HttpVerbs.Delete)]
		public JsonResult DeleteGocChiaSe(int id)
		{
			var deleteItem = _repGocChiaSe.SelectByID(id);
			var fileName = deleteItem.HinhMinhHoa;
			var result = _repGocChiaSe.Delete(id);
			if (result)
			{
				if (fileName != "noimages.jpg")
				{
					var fullPath = Server.MapPath("/Content/Images/Items/" + fileName);
					DeleteFile(fullPath);
				}
			}
			return Json(new{
				Success = result,
				RecordCount = _repGocChiaSe.SelectAll().Count,
				DeleteId = id
			});
		}

		public ActionResult DetailOfGocChiaSe(int id)
		{
			var data = new DataTransferViewModel
			{
				EnumViewModelType = EnumViewModel.ADMIN_DETAILOF_GOCCHIASE,
				IntID = id
			};
			return View(CreateViewModel(data));
		}

		[ValidateInput(false)]
		[AcceptVerbs(HttpVerbs.Post)]
		public ActionResult UpdateGocChiaSe(FormCollection forms)
		{
			var id = forms.GetValues("GocChiaSe_ID")[0];
			var daduyet = bool.Parse(forms.GetValues("GocChiaSe_DaDuyet")[0]);
			
			var editItem = _repGocChiaSe.SelectByID(int.Parse(id));
            editItem.DaDuyet = daduyet;
			
			var result = _repGocChiaSe.Save();
			var data = new DataTransferViewModel
			{
				EnumViewModelType = EnumViewModel.ADMIN_DETAILOF_GOCCHIASE,
				IntID = int.Parse(id.Replace(".", "").Replace(",", "")),
				InfoText = result ? "Item has been updated" : "Cannot update this item"
			};
			return View("DetailOfGocChiaSe", CreateViewModel(data));
		}


		[ValidateInput(false)]
		[AcceptVerbs(HttpVerbs.Post)]
		public ActionResult InsertGocChiaSe(FormCollection forms)
		{
			string tieude = forms.GetValues("GocChiaSe_TieuDe")[0];
			string noidung = forms.GetValues("GocChiaSe_NoiDung")[0];
			string noidungrutgon = StringHelper.Truncate(forms.GetValues("GocChiaSe_NoiDungRutGon")[0], WebConfiguration.GocChiaSeSortDescription);
			var file = Request.Files["GocChiaSe_HinhMinhHoa"];
			string refName = "";
			var newItem = new GocChiaSe
			{
				TieuDe = tieude,
				NoiDung = noidung,
				NoiDungRutGon = noidungrutgon,
				HinhMinhHoa = "noimages.jpg",
                NgayDang = DateTime.Now
			};
			var result = _repGocChiaSe.Insert(newItem);
			if (result)
			{
				if (file.ContentLength != 0)
				{
					result = UploadFile(file, "/Content/Images/Items/", newItem.ID.ToString(), ref refName);
					if (result)
					{
						var addedItem = _repGocChiaSe.SelectByID(newItem.ID);
						addedItem.HinhMinhHoa = refName;
						_repGocChiaSe.Save();
					}
				}
			}
			var data = new DataTransferViewModel
			{
				EnumViewModelType = EnumViewModel.ADMIN_GOCCHIASE,
				InfoText = result ? "New item has been added" : "Cannot insert new item"
			};
			return View("SelectGocChiaSe", CreateViewModel(data));
		}


	}
}